package com.unidt.www.module.system.service.organ;

import com.unidt.www.framework.common.util.object.ConvertUtil;
import com.unidt.www.framework.common.util.tree.TreeUtil;
import com.unidt.www.module.system.dal.dataobject.organ.OrganCodeDo;
import com.unidt.www.module.system.dal.mysql.organ.OrganCodeMapper;
import com.unidt.www.module.system.service.organ.dto.OrganCodeDto;
import com.unidt.www.module.system.service.organ.dto.OrganCodeTreeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author sk
 * @Description:
 * @Date 2022/12/8 10:48
 **/
@Service
public class OrganCodeServiceImpl implements IOrganCodeService {

    @Autowired
    OrganCodeMapper organCodeMapper;

    @Override
    public OrganCodeDto getById(Long organId) {

        OrganCodeDo codeDto = this.organCodeMapper.selectById(organId);
        return    ConvertUtil.convert(codeDto,OrganCodeDto.class);
    }

    @Override
    public List<OrganCodeDto> getChildren(Long organId) {

        if(Objects.isNull(organId)){
            organId = 0L;
        }
        List<OrganCodeDo> children = this.organCodeMapper.getChildren(organId);

        return ConvertUtil.convertList(children,OrganCodeDto.class);
    }

    @Override
    public List<OrganCodeTreeDto> getTree(Long organId) {

        List<OrganCodeDo> list = this.organCodeMapper.getAll(organId);

        List<OrganCodeTreeDto> treeList = new ArrayList<>();

        if(!list.isEmpty()){
            list.forEach(o->{
              treeList.add(ConvertUtil.convert(o,OrganCodeTreeDto.class));
            });
        }
        return TreeUtil.list2Tree(treeList);
    }

    @Override
    public List<OrganCodeDo> getAll(Integer level) {
        return this.organCodeMapper.getAll(level);
    }


}
